package com.esig.Model;


import com.esig.View.GameFrame;
import com.esig.util.Direction;
import com.esig.util.State;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 角色类
 */
public abstract class Role extends GameObject {
    //打印到目的地的左上和右下坐标
//    public int dx1 = (int) (Math.random() * (820 - 80) + 80);  //900-80
//    public int dy1 = (int) (Math.random() * (620 - 300) + 300); //700-80
    public int dx1 = 300;  //900-80
    public int dy1 = 400;  //900-80

    public int dx2 = dx1 + 80;
    public int dy2 = dy1 + 80;

    //从来源地复制的左上和右下坐标
    public int sx1 = 240;
    public int sy1 = 0;
    public int sx2 = 320;
    public int sy2 = 80;


    public int sDy = 0;//跳跃时阴影和人物的高度距离

    //走步速度
    public int walkSpeed = 4;
    //跑步速度
    public int runSpeed = 6;
    //方向 默认右(左右两个方向！)
    public Direction direction = Direction.RIGHT;
    //攻击力 默认0
    public int attack = 0;
    //判断是否处理攻击那一帧
    public boolean attackOnly = false;
    //实现攻击只执行一次
    public int attackNum = 0;
    //实现攻击只执行一次 通过sx1判断是否换帧了（图）
    public int Bsx1;

    //两个方向图片(左3张右3张)
    public String leftImg_0;
    public String leftImg_1;
    public String leftImg_2;

    public String rightImg_0;
    public String rightImg_1;
    public String rightImg_2;

    /**
     * 状态（站立、行走、跑步、跳跃、受伤、倒地、死亡）
     * <p>
     * 攻击状态：
     * 站立（行走）攻击一段、二段、三段
     * 跳跃攻击
     * 冲刺攻击
     * <p>
     * 应该还有技能状态 技能状态一、二、三，技能状态也分一二段
     */
    public State state = State.STAND;

    //swing计时器 图片
    public Timer ImgTimer;
    //swing计时器 位移
    public Timer ChangeTimer;
    //全局的变量，用来当作图片切换计时器的参数
    public String imgTimerParameter = "右行走更改图片";
    //用来当作位移、跳跃过计时器的参数
    public String changeTimerParameter = null;

    //构造方法 带定位x1，y1
    public Role(String img, int dx1, int dy1, GameFrame gameFrame, String leftImg_0, String leftImg_1, String leftImg_2, String rightImg_0, String rightImg_1, String rightImg_2) {
        super(img, dx1, dy1, gameFrame);
        this.leftImg_0 = leftImg_0;
        this.leftImg_1 = leftImg_1;
        this.leftImg_2 = leftImg_2;
        this.rightImg_0 = rightImg_0;
        this.rightImg_1 = rightImg_1;
        this.rightImg_2 = rightImg_2;
    }

    //不带定位的构造方法，因为定位通过随机值设置了
    public Role(String img, GameFrame gameFrame, String leftImg_0, String leftImg_1, String leftImg_2, String rightImg_0, String rightImg_1, String rightImg_2) {
        super(img, gameFrame);
        this.leftImg_0 = leftImg_0;
        this.leftImg_1 = leftImg_1;
        this.leftImg_2 = leftImg_2;
        this.rightImg_0 = rightImg_0;
        this.rightImg_1 = rightImg_1;
        this.rightImg_2 = rightImg_2;
    }

    //构造方法 带精灵图截取
    public Role(String img, int dx1, int dy1, GameFrame gameFrame, int sx1, int sy1, int sx2, int sy2, String leftImg_0, String leftImg_1, String leftImg_2, String rightImg_0, String rightImg_1, String rightImg_2) {
        super(img, dx1, dy1, gameFrame);

        this.sx1 = sx1;
        this.sy1 = sy1;
        this.sx2 = sx2;
        this.sy2 = sy2;

        this.leftImg_0 = leftImg_0;
        this.leftImg_1 = leftImg_1;
        this.leftImg_2 = leftImg_2;
        this.rightImg_0 = rightImg_0;
        this.rightImg_1 = rightImg_1;
        this.rightImg_2 = rightImg_2;
    }

    @Override
    public abstract void paintSelf(Graphics g);

    @Override
    public abstract Rectangle getRec();


    public void leftWalk() {
        direction = Direction.LEFT;
        if (state == State.STAND || state == State.WALK) {
            imgTimerParameter = "左行走更改图片";

            setImg(rightImg_0);

            dx1 -= walkSpeed;
            dx2 -= walkSpeed;
            walkDown();
        } else if (state == State.JUMP) {
            dx1 -= walkSpeed;
            dx2 -= walkSpeed;
        } else if (state == State.RUN) {
            imgTimerParameter = "左跑步更改图片";

            dx1 -= runSpeed;
            dx2 -= runSpeed;
            //切换图片的方法，左右更改sx1等
            imgOnStart();

            //边界值的方法
            boundary();
        }



    }

    public void rightWalk() {
        direction = Direction.RIGHT;
        if (state == State.STAND || state == State.WALK) {
            imgTimerParameter = "右行走更改图片";

            setImg(leftImg_0);

            dx1 += walkSpeed;
            dx2 += walkSpeed;

            walkDown();
        } else if (state == State.JUMP) {
            dx1 += walkSpeed;
            dx2 += walkSpeed;
        } else if (state == State.RUN) {
            imgTimerParameter = "右跑步更改图片";

            dx1 += runSpeed;
            dx2 += runSpeed;

            //切换图片的方法，左右更改sx1等
            imgOnStart();

            //边界值的方法
            boundary();
        }


    }

    public void upWalk() {
        if (state == State.STAND || state == State.WALK || state == State.RUN) {
            sy1 = 0;
            sy2 = 80;
            dy1 -= walkSpeed;
            dy2 -= walkSpeed;

            walkDown();
        }

    }

    public void downWalk() {
        if (state == State.STAND || state == State.WALK || state == State.RUN) {
            sy1 = 0;
            sy2 = 80;
            dy1 += walkSpeed;
            dy2 += walkSpeed;

            walkDown();
        }
    }

    //图片切换的冷却时间为40ms，因为人是要一直移动的，但是精灵图的切换需要一些延迟才合理 之前写的，不太好
    public void walkDown() {
        //设置行走状态
        state = State.WALK;
        if (direction == Direction.RIGHT) {
            imgTimerParameter = "右行走更改图片";
        } else {
            imgTimerParameter = "左行走更改图片";
        }

        sy1 = 0;
        sy2 = 80;
        //边界值的方法
        boundary();
        //计时器启动 切换图片的方法，左右更改sx1等 实现每150ms切换图片
        imgOnStart();

    }

    public void attackOne() {
        if (state == State.STAND || state == State.WALK || state == State.ATTACK_THREE) {
            state = State.ATTACK_ONE;

            sy1 = 80;
            sy2 = 160;

            if (direction == Direction.RIGHT) {
                imgTimerParameter = "右攻击一段更改图片";
                sx1 = 0;
                sx2 = 80;
                dx1 += runSpeed;
                dx2 += runSpeed;
            } else {
                imgTimerParameter = "左攻击一段更改图片";
                sx1 = 720;
                sx2 = 800;
                dx1 -= runSpeed;
                dx2 -= runSpeed;
            }
            //切换图片的方法，左右更改sx1等
            imgOnStart();

            //边界值的方法
            boundary();
        }
    }

    public void attackTwo() {
        if (state == State.ATTACK_ONE) {
            state = State.ATTACK_TWO;

            sy1 = 80;
            sy2 = 160;

            attackNum = 0; //设置处于可以攻击状态，因为一到二的普攻是没有停止图片的

            if (direction == Direction.RIGHT) {

                imgTimerParameter = "右攻击二段更改图片";
                dx1 += runSpeed;
                dx2 += runSpeed;
            } else {

                imgTimerParameter = "左攻击二段更改图片";
                dx1 -= runSpeed;
                dx2 -= runSpeed;
            }
            attackNum = 0; //设置处于可以攻击状态，因为二到三的普攻是没有停止图片的
            //切换图片的方法，左右更改sx1等
            imgOnStart();

            //边界值的方法
            boundary();
        }
    }

    public void attackThree() {
        if (state == State.ATTACK_TWO) {
            state = State.ATTACK_THREE;

            sy1 = 0;
            sy2 = 80;


            if (direction == Direction.RIGHT) {
                imgTimerParameter = "右攻击三段更改图片";
                sx1 = 720;
                sx2 = 800;
                dx1 += runSpeed;
                dx2 += runSpeed;
            } else {
                imgTimerParameter = "左攻击三段更改图片";
                sx1 = 0;
                sx2 = 80;
                dx1 -= runSpeed;
                dx2 -= runSpeed;
            }
            attackNum = 0; //设置处于可以攻击状态，因为二到三的普攻是没有停止图片的
            //切换图片的方法，左右更改sx1等
            imgOnStart();

            //边界值的方法
            boundary();
        }
    }

    public void attackRun() {
        if (state == State.RUN) {
            state = State.ATTACK_RUN;

            sy1 = 240;
            sy2 = 320;

            if (direction == Direction.RIGHT) {
                setImg(leftImg_1);
                changeTimerParameter = "右跑步攻击";
                sx1 = 0;
                sx2 = 80;
            } else {
                setImg(rightImg_1);
                changeTimerParameter = "左跑步攻击";
                sx1 = 720;
                sx2 = 800;
            }

            changeOnStart();
            //切换图片的方法，左右更改sx1等
            imgOnStart();
            boundary();
        }
    }

    public void attackJump() {
        if (state == State.JUMP) {
            state = State.ATTACK_JUMP;
            sy1 = 480;
            sy2 = 560;
            if (direction == Direction.RIGHT) {
                setImg(leftImg_1);
                imgTimerParameter = "右跳跃攻击更改图片";
                sx1 = 160;
                sx2 = 240;
            } else {
                setImg(rightImg_1);
                imgTimerParameter = "左跳跃攻击更改图片";
                sx1 = 560;
                sx2 = 640;
            }
            imgOnStart();
            boundary();
        }
    }

    public void killOneOne() {
        if (state == State.STAND || state == State.WALK || state == State.RUN ||state == State.ATTACK_THREE) {
            state = State.SKILL_ONE_ONE;

            sy1 = 0;
            sy2 = 80;

            if (direction == Direction.RIGHT) {
                setImg(leftImg_2);
                changeTimerParameter = "右技能1第1段";
                sx1 = 0;
                sx2 = 80;
                dx1 += runSpeed;
                dx2 += runSpeed;
            } else {
                setImg(rightImg_2);
                changeTimerParameter = "左技能1第1段";
                sx1 = 720;
                sx2 = 800;
                dx1 -= runSpeed;
                dx2 -= runSpeed;
            }

            changeOnStart();
            //切换图片的方法，左右更改sx1等
            imgOnStart();

        }
    }

    public void killTwoOne() {
        if (state == State.STAND || state == State.WALK || state == State.RUN || state == State.SKILL_TWO_TWO) {
            state = State.SKILL_TWO_ONE;

            sy1 = 0;
            sy2 = 80;

            if (direction == Direction.RIGHT) {
                setImg(leftImg_2);
                imgTimerParameter = "右技能2第1段更改图片";
                sx1 = 0;
                sx2 = 80;
            } else {
                setImg(rightImg_2);

                imgTimerParameter = "左技能2第1段更改图片";
                sx1 = 720;
                sx2 = 800;
            }
            //切换图片的方法，左右更改sx1等
            imgOnStart();
        }
    }

    public void killTwoTwo() {
        if (state == State.STAND || state == State.WALK || state == State.RUN || state == State.SKILL_TWO_ONE) {
            state = State.SKILL_TWO_TWO;

            sy1 = 0;
            sy2 = 80;

            if (direction == Direction.RIGHT) {
                setImg(leftImg_2);
                imgTimerParameter = "右技能2第2段更改图片";
                sx1 = 400;
                sx2 = 480;
            } else {
                setImg(rightImg_2);

                imgTimerParameter = "左技能2第2段更改图片";
                sx1 = 320;
                sx2 = 400;
            }
            //切换图片的方法，左右更改sx1等
            imgOnStart();
        }
    }

    public void injuredOne() {
        state = State.INJUREDONE;

        sy1 = 400;
        sy2 = 480;

        if (direction == Direction.RIGHT) {
            setImg(leftImg_1);
            imgTimerParameter = "右受伤一段更改图片";
            sx1 = 0;
            sx2 = 80;
        } else {
            setImg(rightImg_1);
            imgTimerParameter = "左受伤一段更改图片";
            sx1 = 720;
            sx2 = 800;
        }
        //切换图片的方法，左右更改sx1等
        imgOnStart();

        //边界值的方法
        boundary();
    }

    public void injuredTwo() {
        state = State.INJUREDTWO;

        sy1 = 400;
        sy2 = 480;

        if (direction == Direction.RIGHT) {
            setImg(leftImg_1);
            imgTimerParameter = "右受伤二段更改图片";
            sx1 = 240;
            sx2 = 320;
        } else {
            setImg(rightImg_1);
            imgTimerParameter = "左受伤二段更改图片";
            sx1 = 560;
            sx2 = 640;
        }
        //切换图片的方法，左右更改sx1等
        imgOnStart();

        //边界值的方法
        boundary();
    }

    public void injuredThree() {
        state = State.INJUREDTHREE;

        sy1 = 240;
        sy2 = 320;

        if (direction == Direction.RIGHT) {
            setImg(leftImg_0);
            imgTimerParameter = "右受伤三段更改图片";
            sx1 = 0;
            sx2 = 80;
        } else {
            setImg(rightImg_0);
            imgTimerParameter = "左受伤三段更改图片";
            sx1 = 720;
            sx2 = 800;
        }
        //切换图片的方法，左右更改sx1等
        imgOnStart();

        //边界值的方法
        boundary();
    }


    /************ 计时器实现图片150ms左右切换 start *******************/
    private class Img implements ActionListener {

        private int jumpP = 0;
        private int jumpE = 80;

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            //1、actionPerformed()在界面工作线程里运行，所以必须迅速返回
            //2、在这里可以直接更新UI

            if (imgTimerParameter == "右行走更改图片") {
                rightWalkS();
            }
            if (imgTimerParameter == "左行走更改图片") {
                leftWalkS();
            }
            if (imgTimerParameter == "左跳跃更改图片") {
                leftJumpS();
            }
            if (imgTimerParameter == "右跳跃更改图片") {
                rightJumpS();
            }
            if (imgTimerParameter == "左攻击一段更改图片") {
                leftAttackOneS();
            }
            if (imgTimerParameter == "右攻击一段更改图片") {
                rightAttackOneS();
            }
            if (imgTimerParameter == "左攻击二段更改图片") {
                leftAttackTwoS();
            }
            if (imgTimerParameter == "右攻击二段更改图片") {
                rightAttackTwoS();
            }
            if (imgTimerParameter == "左攻击三段更改图片") {
                leftAttackThreeS();
            }
            if (imgTimerParameter == "右攻击三段更改图片") {
                rightAttackThreeS();
            }
            if (imgTimerParameter == "左跑步更改图片") {
                leftRunS();
            }
            if (imgTimerParameter == "右跑步更改图片") {
                rightRunS();
            }
            if (imgTimerParameter == "左跑步攻击更改图片") {
                leftRunAttackS();
            }
            if (imgTimerParameter == "右跑步攻击更改图片") {
                rightRunAttackS();
            }
            if (imgTimerParameter == "左跳跃攻击更改图片") {
                leftJumpAttackS();
            }
            if (imgTimerParameter == "右跳跃攻击更改图片") {
                rightJumpAttackS();
            }
            if (imgTimerParameter == "左技能1第1段更改图片") {
                leftKillOneS();
            }
            if (imgTimerParameter == "右技能1第1段更改图片") {
                rightKillOneS();
            }
            if (imgTimerParameter == "左技能2第1段更改图片") {
                leftKillTwoOneS();
            }
            if (imgTimerParameter == "右技能2第1段更改图片") {
                rightKillTwoOneS();
            }
            if (imgTimerParameter == "左技能2第2段更改图片") {
                leftKillTwoTwoS();
            }
            if (imgTimerParameter == "右技能2第2段更改图片") {
                rightKillTwoTwoS();
            }
            if (imgTimerParameter == "左受伤一段更改图片") {
                leftInjuredOneS();
            }
            if (imgTimerParameter == "右受伤一段更改图片") {
                rightInjuredOneS();
            }
            if (imgTimerParameter == "左受伤二段更改图片") {
                leftInjuredTwoS();
            }
            if (imgTimerParameter == "右受伤二段更改图片") {
                rightInjuredTwoS();
            }
            if (imgTimerParameter == "左受伤三段更改图片") {
                leftInjuredThreeS();
            }
            if (imgTimerParameter == "右受伤三段更改图片") {
                rightInjuredThreeS();
            }


//            System.out.println("开始图片切换定时器");
        }

    }


    //开始定时器
    public void imgOnStart() {
        if (ImgTimer != null) return;

        ActionListener task = new Img();

        switch (imgTimerParameter) {  //图片切换定时器
            case "右行走更改图片":
            case "左行走更改图片":
            case "左跳跃更改图片":
            case "右跳跃更改图片":
            case "左攻击一段更改图片":
            case "右攻击一段更改图片":
            case "左攻击二段更改图片":
            case "右攻击二段更改图片":
            case "左攻击三段更改图片":
            case "右攻击三段更改图片":
            case "左跑步更改图片":
            case "右跑步更改图片":
            case "左跑步攻击更改图片":
            case "右跑步攻击更改图片":
            case "左跳跃攻击更改图片":
            case "右跳跃攻击更改图片":
            case "左技能1第1段更改图片":
            case "右技能1第1段更改图片":
            case "左技能2第1段更改图片":
            case "右技能2第1段更改图片":
            case "左技能2第2段更改图片":
            case "右技能2第2段更改图片":
            case "左受伤一段更改图片":
            case "右受伤一段更改图片":
            case "左受伤二段更改图片":
            case "右受伤二段更改图片":
            case "左受伤三段更改图片":
            case "右受伤三段更改图片":

                ImgTimer = new Timer(150, task);
                break;
            default:
                System.out.println("输入不正确...");
                break;
        }


        ImgTimer.start();

    }

    //停止定时器
    public void imgOnEnd() {
        if (ImgTimer != null) {
            ImgTimer.stop();
            ImgTimer = null;
            attackNum = 0; //让攻击的次数为0
        }
        System.out.println("关闭图片切换定时器");
    }

    /************ 计时器实现图片150ms左右切换 end *******************/

    /************ 计时器实现位移、跳跃过渡等 start *******************/
    private class Change implements ActionListener {

        private int jumpP = 0;   //跳跃的顶点 到80为上限
        private int jumpE = 80;  //跳跃的起点 到0为下限

        private int runTime = 0; //跑步攻击的花费的时间

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            //1、actionPerformed()在界面工作线程里运行，所以必须迅速返回
            //2、在这里可以直接更新UI
            if (changeTimerParameter == "左跳跃") {
                if (state != State.ATTACK_JUMP) {
                    imgTimerParameter = "左跳跃更改图片";
                }

                if (jumpP >= 80) {
                    dy1 += runSpeed;
                    dy2 += runSpeed;
                    sDy -= runSpeed; //跳跃时阴影和人物的距离
                    jumpE -= runSpeed;
                    if (jumpE <= 0) {
                        imgOnEnd();
                        changeOnEnd();
                        leftStandS();
                        imgTimerParameter = "左行走更改图片";
                    }
                    return;
                }
                dy1 -= runSpeed;
                dy2 -= runSpeed;
                sDy += runSpeed; //跳跃时阴影和人物的距离
                jumpP += runSpeed;
            }


            if (changeTimerParameter == "右跳跃") {
                if (state != State.ATTACK_JUMP) {
                    imgTimerParameter = "右跳跃更改图片";
                }
                if (jumpP >= 80) {
                    dy1 += runSpeed;
                    dy2 += runSpeed;
                    sDy -= runSpeed; //跳跃时阴影和人物的距离
                    jumpE -= runSpeed;
                    if (jumpE <= 0) {
                        imgOnEnd();
                        changeOnEnd();
                        rightStandS();
                        imgTimerParameter = "右行走更改图片";
                    }
                    return;
                }
                dy1 -= runSpeed;
                dy2 -= runSpeed;
                sDy += runSpeed; //跳跃时阴影和人物的距离
                jumpP += runSpeed;
            }

            if (changeTimerParameter == "左技能1第1段") {
                imgTimerParameter = "左技能1第1段更改图片";

                if (jumpP >= 80) {
                    dy1 += runSpeed;
                    dy2 += runSpeed;
                    sDy -= runSpeed; //跳跃时阴影和人物的距离
                    jumpE -= runSpeed;
                    if (jumpE <= 0) {
                        setImg(rightImg_0);
                        imgOnEnd();
                        changeOnEnd();
                        leftStandS();
                        imgTimerParameter = "左行走更改图片";
                    }
                    return;
                }
                dy1 -= runSpeed;
                dy2 -= runSpeed;
                sDy += runSpeed; //跳跃时阴影和人物的距离
                jumpP += runSpeed;
            }

            if (changeTimerParameter == "右技能1第1段") {
                imgTimerParameter = "右技能1第1段更改图片";

                if (jumpP >= 80) {
                    dy1 += runSpeed;
                    dy2 += runSpeed;
                    sDy -= runSpeed; //跳跃时阴影和人物的距离
                    jumpE -= runSpeed;
                    if (jumpE <= 0) {
                        setImg(leftImg_0);
                        imgOnEnd();
                        changeOnEnd();
                        rightStandS();
                        imgTimerParameter = "右行走更改图片";
                    }
                    return;
                }
                dy1 -= runSpeed;
                dy2 -= runSpeed;
                sDy += runSpeed; //跳跃时阴影和人物的距离
                jumpP += runSpeed;
            }

            if (changeTimerParameter == "左跑步攻击") {
                imgTimerParameter = "左跑步攻击更改图片";
                dx1 -= 7;
                dx2 -= 7;
                //跑步攻击动画所需要的时间
                runTime += 100;
                if (runTime >= 2200) {
                    setImg(rightImg_0);
                    imgOnEnd();
                    changeOnEnd();
                    leftStandS();
                    imgTimerParameter = "左行走更改图片";
                }
            }

            if (changeTimerParameter == "右跑步攻击") {
                imgTimerParameter = "右跑步攻击更改图片";
                dx1 += 7;
                dx2 += 7;
                //跑步攻击动画所需要的时间
                runTime += 100;
                if (runTime >= 2200) {
                    setImg(rightImg_0);
                    imgOnEnd();
                    changeOnEnd();
                    rightStandS();
                    imgTimerParameter = "右行走更改图片";
                }
            }


//            System.out.println("开始位移定时器");

            if (changeTimerParameter == "受伤浮空") {
                if (jumpP >= 80) {
                    dy1 += runSpeed;
                    dy2 += runSpeed;
                    jumpE -= runSpeed;
                    if (jumpE <= 0) {
                        changeOnEnd();
                        //这里可以设置到地板在切换到最后的倒地帧 但是我别的地方用了，改起来有点麻烦，还是算了
                    }
                    return;
                }
                dy1 -= runSpeed;
                dy2 -= runSpeed;
                jumpP += runSpeed;
            }
        }

    }

    //开始定时器
    public void changeOnStart() {
        if (ChangeTimer != null) return;

        ActionListener task2 = new Change();
        switch (changeTimerParameter) {
            case "左跳跃":
            case "右跳跃":
            case "左技能1第1段":
            case "右技能1第1段":
            case "左跑步攻击":
            case "右跑步攻击":
            case "受伤浮空":

                ChangeTimer = new Timer(30, task2);
                break;
        }
        ChangeTimer.start();
    }

    //停止定时器
    public void changeOnEnd() {
        if (ChangeTimer != null) {
            ChangeTimer.stop();
            ChangeTimer = null;
        }
        System.out.println("关闭位移定时器");
    }

    /************ 计时器实现位移及跳跃时的过渡等等 end *******************/


    /************ 各种状态，如左站立状态，右站立状态。。。左跳跃、右跳跃等等 start *******************/
    public void leftStandS() {
        state = State.STAND;
        setImg(rightImg_0);
        sy1 = 0;
        sy2 = 80;
        sx1 = 480;
        sx2 = 560;
    }

    public void rightStandS() {
        state = State.STAND;
        setImg(leftImg_0);
        sy1 = 0;
        sy2 = 80;
        sx1 = 240;
        sx2 = 320;
    }

    public void leftWalkS() {
        sy1 = 0;
        sy2 = 80;

        state = State.WALK;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 <= 160) {
            sx1 = 480;
            sx2 = 560;
        }
    }

    public void rightWalkS() {
        sy1 = 0;
        sy2 = 80;

        state = State.WALK;
        sx1 += 80;
        sx2 += 80;
        if (sx1 >= 560) {
            sx1 = 320;
            sx2 = 400;
        }
    }

    public void leftJumpS() {
        state = State.JUMP;
        sy1 = 480;
        sy2 = 560;

        sx1 = 480;
        sx2 = 560;
    }


    public void rightJumpS() {
        state = State.JUMP;
        sy1 = 480;
        sy2 = 560;

        sx1 = 240;
        sx2 = 320;
    }

    public void leftAttackOneS() {
        state = State.ATTACK_ONE;
        sy1 = 80;
        sy2 = 160;

        sx1 = 640;
        sx2 = 720;

        attackOnly(5); //只攻击一次，攻击力为5


    }

    public void rightAttackOneS() {
        state = State.ATTACK_ONE;
        sy1 = 80;
        sy2 = 160;

        sx1 = 80;
        sx2 = 160;

        attackOnly(5);


    }

    public void leftAttackTwoS() {
        state = State.ATTACK_TWO;


        if (sx1 != 160) { //等于160时，说明播完了
            sx1 -= 80;
            sx2 -= 80;
        }
        //这一帧是攻击帧
        if (sx1 == 320) {
            attackOnly(6);
        }
    }

    public void rightAttackTwoS() {
        state = State.ATTACK_TWO;
        if (sx1 != 560) {
            sx1 += 80;
            sx2 += 80;
        }

        //这一帧是攻击帧
        if (sx1 == 400) {
            attackOnly(6);
        }
    }

    public void leftAttackThreeS() {
        state = State.ATTACK_THREE;

        sx1 = 80;
        sx2 = 160;
        attackOnly(7);

    }

    public void rightAttackThreeS() {
        state = State.ATTACK_THREE;

        sx1 = 640;
        sx2 = 720;
        attackOnly(7);

    }

    public void leftRunS() {
        sy1 = 160;
        sy2 = 240;

        state = State.RUN;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 < 560) {
            sx1 = 720;
            sx2 = 800;
        }

    }


    public void rightRunS() {
        sy1 = 160;
        sy2 = 240;

        state = State.RUN;
        sx1 += 80;
        sx2 += 80;
        if (sx1 > 160) {
            sx1 = 0;
            sx2 = 80;
        }

    }

    public void leftKillOneS() {
        sy1 = 240;
        sy2 = 320;

        state = State.SKILL_ONE_ONE;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 <= 80) {
            sx1 = 160;
            sx2 = 240;
        }
        if (sx1 == 720 || sx1 == 640) {
            attackOnly(6);
        }
    }


    public void rightKillOneS() {
        sy1 = 240;
        sy2 = 320;

        state = State.SKILL_ONE_ONE;
        sx1 += 80;
        sx2 += 80;
        if (sx1 >= 560) {
            sx1 = 0;
            sx2 = 80;
        }

        if (sx1 == 80 || sx1 == 160) {
            attackOnly(6);
        }
    }

    public void leftKillTwoOneS() {
        sy1 = 0;
        sy2 = 80;

        state = State.SKILL_TWO_ONE;
        sx1 += 80;
        sx2 += 80;
        if (sx1 >= 400) {
            sx1 = 0;
            sx2 = 80;
        }
    }

    public void rightKillTwoOneS() {
        sy1 = 0;
        sy2 = 80;

        state = State.SKILL_TWO_ONE;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 <= 320) {
            sx1 = 720;
            sx2 = 800;
        }
    }

    public void leftKillTwoTwoS() {
        sy1 = 0;
        sy2 = 80;

        state = State.SKILL_TWO_TWO;
        sx1 += 80;
        sx2 += 80;
        if (sx1 > 720) {
            sx1 = 400;
            sx2 = 480;
        }
    }

    public void rightKillTwoTwoS() {
        sy1 = 0;
        sy2 = 80;

        state = State.SKILL_TWO_TWO;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 < 0) {
            sx1 = 320;
            sx2 = 400;
        }
    }

    public void leftRunAttackS() {
        sy1 = 240;
        sy2 = 320;

        state = State.ATTACK_RUN;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 == 480 || sx1 == 560 || sx1 == 640) {
            attackOnly(5);
        }
    }

    public void rightRunAttackS() {
        sy1 = 240;
        sy2 = 320;

        state = State.ATTACK_RUN;
        sx1 += 80;
        sx2 += 80;
        if (sx1 == 160 || sx1 == 240 || sx1 == 320) {
            attackOnly(5);
        }
    }

    public void leftJumpAttackS() {
        sy1 = 480;
        sy2 = 560;

        state = State.ATTACK_JUMP;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 == 320) {
            attackOnly(8);
        }
    }

    public void rightJumpAttackS() {
        attack = 7;

        sy1 = 480;
        sy2 = 560;

        state = State.ATTACK_JUMP;
        sx1 += 80;
        sx2 += 80;
        if (sx1 == 400) {
            attackOnly(8);
        }
    }

    public void leftInjuredOneS() { //受伤一段
        sy1 = 400;
        sy2 = 480;

        state = State.INJUREDONE;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 < 560) {
            sx1 = 720;
            sx2 = 800;
        }
    }

    public void rightInjuredOneS() {
        sy1 = 400;
        sy2 = 480;

        state = State.INJUREDONE;
        sx1 += 80;
        sx2 += 80;
        if (sx1 > 160) {
            sx1 = 0;
            sx2 = 80;
        }
    }

    public void leftInjuredTwoS() { //受伤二段
        sy1 = 400;
        sy2 = 480;

        state = State.INJUREDTWO;
        sx1 -= 80;
        sx2 -= 80;
        if (sx1 < 400) {
            sx1 = 480;
            sx2 = 560;
        }
    }

    public void rightInjuredTwoS() {
        sy1 = 400;
        sy2 = 480;

        state = State.INJUREDTWO;
        sx1 += 80;
        sx2 += 80;
        if (sx1 > 320) {
            sx1 = 240;
            sx2 = 320;
        }
    }

    public void leftInjuredThreeS() { //受伤三段
        sy1 = 320;
        sy2 = 400;

        state = State.INJUREDTHREE;

        if (sx1 != 400) {
            sx1 -= 80;
            sx2 -= 80;
        }
    }

    public void rightInjuredThreeS() {
        sy1 = 320;
        sy2 = 400;

        state = State.INJUREDTHREE;

        if (sx1 != 320) {
            sx1 += 80;
            sx2 += 80;
        }
    }

    /************ 各种状态，如左站立状态，右站立状态。。。左跳跃、右跳跃等等 end *******************/


    //设置边界，防止人物出边界范围
    public void boundary() {
        if (this.dx1 <= 0) {
            this.dx1 = 0;
            this.dx2 = 80;
        }
        if (this.dx2 >= 900) {
            this.dx1 = 820;
            this.dx2 = 900;
        }
        if (this.dy1 <= 180) {
            this.dy1 = 180;
            this.dy2 = 260;
        }
        if (this.dy2 >= 600) {
            this.dy1 = 520;
            this.dy2 = 600;
        }

    }

    //攻击一次的判定，通过动画切图的时间150ms除于重绘时间30ms ==5时判断它结束！
    public void attackOnly(int attack) {
        if (attackNum == 0) {
            this.attack = attack;
            attackOnly = true; //设置这一帧为攻击帧
            ++attackNum;   //一直加，等图片切换定时器结束才变成0
        }

    }
}
